package com.yjhealth.libs.core.core.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yjhealth.libs.core.R;
import com.yjhealth.libs.core.core.CoreEvent;
import com.yjhealth.libs.core.net.utils.NetworkErrorUtil;
import com.yjhealth.libs.core.thirdpart.statusbarutil.StatusBarUtil;
import com.yjhealth.libs.core.toast.ToastUtil;
import com.yjhealth.libs.core.view.dialog.LoadingDialog;
import com.yjhealth.libs.core.view.loadview.LoadViewHelper;
import com.yjhealth.libs.core.view.titlebar.BsoftToolbar;
import com.yjhealth.libs.core.view.titlebar.config.TitleButtonConfig;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by chenkai on 2018/6/1.
 */

public abstract class CoreActivity extends CoreLoadViewActivity {
    /*Util*/
    /*Flag*/
    public Fragment currentFragment;
    public CoreActivity activity;
    /*View*/
    private LoadingDialog loadingDialog;

    protected Toolbar yjhealthCoreToolbar;
    protected TextView yjhealthCoreTvTitle;

    //同yjhealthCoreToolbar AND yjhealthCoreTvTitle
    protected BsoftToolbar yjhealthCoreBosftToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.activity = this;
        // EventBus
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        setStatusBar();
    }


    @Override
    protected void onDestroy() {
        //EventBus
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        //loadingDialog
        dismissLoadingDialog();
        super.onDestroy();
    }

    protected void initLayout() {
        initToolBar();
        super.initLoadView();
    }

    protected void initToolBar() {
        //Toolbar
        yjhealthCoreTvTitle = findViewById(R.id.yjhealthCoreTvTitle);
        yjhealthCoreToolbar = findViewById(R.id.yjhealthCoreToolbar);
        if (yjhealthCoreToolbar != null) {
            yjhealthCoreToolbar.setTitle("");
            setSupportActionBar(yjhealthCoreToolbar);
            yjhealthCoreToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }

        //BsoftToolbar
        yjhealthCoreBosftToolbar = findViewById(R.id.yjhealthCoreBosftToolbar);
        if (yjhealthCoreBosftToolbar != null) {
            yjhealthCoreBosftToolbar.setLisntener(new BsoftToolbar.OnBsoftToolbarLisntener() {
                @Override
                public void onItemClick(TitleButtonConfig config) {

                }

                @Override
                public void onBack() {
                    onBackPressed();
                }

                @Override
                public void updateStatusBar() {
                    setStatusBar();
                }
            });
        }
    }


    protected void setStatusBar() {
        StatusBarUtil.setDarkMode(this);
        StatusBarUtil.setColor(this, Color.GRAY, 0);
    }


    //************************************ loadingDialog *******************************************
    public void showLoadingDialog() {
        showLoadingDialog(0, null);
    }

    public void showLoadingDialog(@DrawableRes int resourceId, String msg) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.newInstance(resourceId, msg);
        }
        loadingDialog.show(activity, "loading");
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismissAllowingStateLoss();
            loadingDialog = null;
        }
    }

    //************************************* show error toast ***************************************
    public void showErrorToast(String errorType, String errorMsg) {
        if (TextUtils.equals(errorType, NetworkErrorUtil.ERROR_TYPE_SHOW)) {
            ToastUtil.toast(errorMsg);
        } else if (!TextUtils.equals(errorType, NetworkErrorUtil.ERROR_FORBIDDEN)) {
            ToastUtil.toast(LoadViewHelper.getMsgIdForError(errorType, 0));
        }
    }

    //*************************************** 切换fragment ******************************************
    public void switchFragment(Fragment targetFragment, @IdRes int resId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(resId, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction.hide(currentFragment).show(targetFragment);
        }
        currentFragment = targetFragment;
        transaction.commitAllowingStateLoss();
    }

    //*************************************** keyboard ******************************************
    public void hideKeyboard() {
        if (null != getCurrentFocus()
                && null != getCurrentFocus().getWindowToken()) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            // 隐藏软键盘
            if (imm != null) {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
            }
        }
    }

    public void showKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(getWindow().getDecorView(), InputMethodManager.SHOW_FORCED);
        }
    }

    public void showKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInputFromInputMethod(editText.getWindowToken(), 0);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void event(CoreEvent coreEvent) {

    }
}
